﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.Service.Contract
{
    [Serializable]
    public class OpenSceneArgs
    {
        /// <summary>
        /// 参数信息，这些参数将用于初始化场景中定义的参数
        /// </summary>
        public Dictionary<string, string> ParametersString { get; set; }

        public OpenSceneArgs()
        {
            ParametersString = new Dictionary<string, string>();
        }

        /// <summary>
        /// 获取参数的值，按照bool型获取
        /// </summary>
        /// <param name="parameterName"></param>
        /// <returns></returns>
        public bool GetBool(string parameterName)
        {
            if (ParametersString.ContainsKey(parameterName))
            {
                bool isTrue = false;
                if (bool.TryParse(ParametersString[parameterName], out isTrue))
                {
                    return isTrue;
                }
            }
            return false;
        }

        public static OpenSceneArgs BuildFromString(string argumentString)
        {
            OpenSceneArgs args = new OpenSceneArgs();
            if (!string.IsNullOrEmpty(argumentString))
            {
                string[] argumentArray = argumentString.Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                for (int index = 0; index < argumentArray.Length; index++)
                {
                    string[] argumentKV = argumentArray[index].Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);
                    args.ParametersString.Add(argumentKV[0], argumentKV[1]);
                }
            }
            return args;
        }

        public static string GetParametersString(OpenSceneArgs args)
        {
            if (args == null
                || args.ParametersString == null)
            {
                return null;
            }

            StringBuilder builder = new StringBuilder();
            foreach (string key in args.ParametersString.Keys)
            {
                builder.AppendFormat("{0}={1}&", key, args.ParametersString[key]);
            }
            return builder.ToString();
        }
    }
}
